#include <iostream>


using namespace std;
class Node{
   public:
        int data;
        Node* next;
    public:
        Node(int val){
            data = val;
            next = NULL;
        }
        void print(){
            Node* temp=this;
            while(temp!=NULL){
                cout<<temp->data<<" ";
                temp=temp->next;
            }
        }
};

Node* reverseList(Node* head,Node* oldHead, Node* newHead=nullptr,int count=0,int n=5) {
    if(count==n){
        head->next=oldHead;
        return newHead;
    }
    Node* next=oldHead->next;
    oldHead->next=newHead;
    newHead=oldHead;
    return (next==nullptr)? newHead : reverseList(head,next,newHead,count+1,n);
}
int main() {
    Node* head = new Node(1);
    Node* tmp=head;
    for(int i=2;i<=7;i++){
        Node* newNode = new Node(i);
        tmp->next=newNode;
        tmp=newNode;
    }
    head->print();
    cout<<"\n";
    Node* newHead=reverseList(head,head,nullptr,0,3);
    newHead->print();
    return 0;
}